<template>
  <div>
    <!-- 已登录 -->
    <div class="header user-info" v-if="user">
      <div class="base-info">
        <div class="left">
          <van-image round class="avatar" fit="cover" :src="userInfo.photo" />
          <span class="name">{{ userInfo.name }}</span>
        </div>
        <div class="right">
          <van-button round size="mini" to="/user/profile">编辑资料</van-button>
        </div>
      </div>
      <div class="data-stats">
        <div class="data-item">
          <div class="count">8</div>
          <div class="text">头条</div>
        </div>
        <div class="data-item">
          <div class="count">1</div>
          <div class="text">关注</div>
        </div>
        <div class="data-item">
          <div class="count">24681</div>
          <div class="text">粉丝</div>
        </div>
        <div class="data-item">
          <div class="count">94399</div>
          <div class="text">获赞</div>
        </div>
      </div>
    </div>

    <!-- 未登录 -->
    <div class="header not-login" v-else>
      <div class="login-btn" @click="$router.push('/login')">
        <img class="mobile-img" src="@/assets/mobile.png" />
        <span class="text">登录 / 注册</span>
      </div>
    </div>

    <!-- 宫格导航 -->
    <van-grid :column-num="2" clickable>
      <van-grid-item>
        <i slot="icon" class="iconfont iconshoucang"></i>
        <span slot="text" class="text">收藏</span>
      </van-grid-item>
      <van-grid-item>
        <i slot="icon" class="iconfont iconlishi"></i>
        <span slot="text" class="text">历史</span>
      </van-grid-item>
    </van-grid>

    <!-- 单元格导航 -->
    <van-cell title="消息通知" is-link />
    <van-cell title="小智同学" is-link class="mb-9" />
    <van-cell
      title="退出登录"
      class="login-out"
      v-if="user"
      @click="loginOut"
      clickable
    />
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getUserInfo } from '@/api/user'

export default {
  name: 'MyIndex',
  data() {
    return {
      userInfo: {}
    }
  },
  computed: {
    ...mapState(['user'])
  },
  created() {
    this.loadUserInfo()
  },
  methods: {
    loginOut() {
      this.$dialog
        .confirm({
          title: '确认退出吗？'
        })
        .then(() => {
          this.$store.commit('setUser', null)
        })
        .catch(() => {
          // on cancel
        })
    },
    async loadUserInfo() {
      try {
        const { data: res } = await getUserInfo()
        this.userInfo = res.data
        // console.log(res)
      } catch (error) {
        this.$toast('获取数据失败,请稍后再试！')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.header {
  height: 361px;
  background: url('~@/assets/banner.png') no-repeat;
  background-size: cover;
}

.not-login {
  display: flex;
  justify-content: center;
  align-items: center;

  .login-btn {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
  }

  .mobile-img {
    width: 132px;
    height: 132px;
    margin-bottom: 15px;
  }

  .text {
    color: #fff;
    font-size: 28px;
  }
}

.user-info {
  .base-info {
    box-sizing: border-box;
    height: 231px;
    padding: 76px 32px 23px;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .left {
      display: flex;
      align-items: center;
      .avatar {
        width: 132px;
        height: 132px;
        margin-right: 18px;
        border: 4px solid #fff;
      }

      .name {
        font-size: 30px;
        color: #fff;
      }
    }
  }

  .data-stats {
    height: 130px;
    display: flex;

    .data-item {
      flex: 1;
      color: #fff;
      display: flex;
      justify-content: center;
      align-items: center;
      flex-direction: column;

      .count {
        font-size: 42px;
        margin-bottom: 10px;
      }
      .text {
        font-size: 25px;
      }
    }
  }
}

.van-grid {
  height: 141px;
  margin-bottom: 9px;

  .van-grid-item {
    height: 100%;

    i.iconfont {
      font-size: 46px;
    }
    .iconshoucang {
      color: #eb5253;
    }
    .iconlishi {
      color: #ff9e1f;
    }
    span.text {
      font-size: 28px;
    }
  }
}

.login-out {
  text-align: center;
  color: #d86262;
}

.mb-9 {
  margin-bottom: 9px;
}
</style>
